<?php
class class_ABMArticulos{
	private $_db; 
	function __construct(){
		$this->config();
	}
	
	function method_getLista($params, $error){
		$this->_db->setQuery("SELECT articulos.*, rubros.descripcion AS rubro, marcas.descripcion AS marca FROM articulos LEFT JOIN rubros ON articulos.cod_rubro = rubros.cod_rubro LEFT JOIN marcas ON articulos.cod_marca = marcas.cod_marca");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$result = array();
		$regs = $this->_db->loadResult();
		$row = array();
    	FOREACH ($regs as $reg){
			array_push($row, $reg->cod_articulo);
			array_push($row, $reg->rubro);			
			array_push($row, $reg->descripcion);
			array_push($row, $reg->marca);
			array_push($row, $reg->cantidad_min);
			array_push($row, $reg->cantidad_max);
			array_push($result, $row);
			$row = array();
		}
		return $result;
    }
    
	function method_btnBorrar($params, $error){
        if (count($params) != 1){ $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        $this->_db->setQuery("DELETE FROM articulos WHERE cod_articulo = '" . $params[0] . "'");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	function method_btnModificar($params, $error){
        if (count($params) != 1){
        $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
		$this->_db->setQuery("SELECT * FROM articulos WHERE cod_articulo = '" . $params[0] . "' LIMIT 1");
		$this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$reg = $this->_db->loadResult();
		return $reg[0];
    }
    
	function method_opAlta($params, $error) {
        if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        $this->_db->setQuery("
        INSERT INTO articulos 
        (cod_articulo, 
        descripcion, 
        cantidad_min, 
        cantidad_max, 
        cod_rubro, 
        cod_marca) 
        VALUES 
        (NULL, 
        '" . $params[0]->descripcion . "', 
        '" . $params[0]->cantidad_min. "', 
        '" . $params[0]->cantidad_max . "', 
        '" . $params[0]->cod_rubro . "', 
        '" . $params[0]->cod_marca . "')
        ");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    
	function method_opModificacion($params, $error){
		if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
	    $this->_db->setQuery("
	    UPDATE articulos SET 
	    descripcion = '" . $params[0]->descripcion. "', 
	    cantidad_min = '" . $params[0]->cantidad_min. "', 
	    cantidad_max = '" . $params[0]->cantidad_max. "', 
	    cod_rubro = '" . $params[0]->cod_rubro. "', 
	    cod_marca = '" . $params[0]->cod_marca. "' 
	    LIMIT 1");
        $this->_db->alter();
		if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    function method_getRubros($params, $error){
    	//if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
		$this->_db->setQuery("SELECT cod_rubro AS value, descripcion AS label FROM rubros");
		$this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$regs = $this->_db->loadResult();
		$result->values = $regs;
		$result->nullValue = true;
		return $result;
    }
    function method_getMarcas($params, $error){
    	//if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
		$this->_db->setQuery("SELECT cod_marca AS value, descripcion AS label FROM marcas");
		$this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		$regs = $this->_db->loadResult();
		$result->values = $regs;
		$result->nullValue = true;
		return $result;
    }
    
	private function config(){
		require_once("conexion.php");
		require_once("DataBase.php");
		$this->_db = new DataBase($SERVIDOR,$USUARIO,$PASSWORD,$BASE);
		mysql_query("SET NAMES utf8");
	}
}
?>